package upgrade

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/upgrade"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var DevOtaModule = cUpgradeDev_ota_module{}

type cUpgradeDev_ota_module struct{}

// GetDevOtaModuleList 获取列表
func (u *cUpgradeDev_ota_module) GetDevOtaModuleList(ctx context.Context, req *upgrade.GetDevOtaModuleListReq) (res *upgrade.GetDevOtaModuleListRes, err error) {
	var reqData = new(model.GetDevOtaModuleListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.UpgradeDevOtaModule().GetDevOtaModuleList(ctx, reqData)
	res = new(upgrade.GetDevOtaModuleListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// GetDevOtaModuleById 获取指定ID数据
func (u *cUpgradeDev_ota_module) GetDevOtaModuleById(ctx context.Context, req *upgrade.GetDevOtaModuleByIdReq) (res *upgrade.GetDevOtaModuleByIdRes, err error) {
	data, err := service.UpgradeDevOtaModule().GetDevOtaModuleById(ctx, req.Id)
	res = new(upgrade.GetDevOtaModuleByIdRes)
	gconv.Scan(data, &res)
	return
}

// AddDevOtaModule 添加数据
func (u *cUpgradeDev_ota_module) AddDevOtaModule(ctx context.Context, req *upgrade.AddDevOtaModuleReq) (res *upgrade.AddDevOtaModuleRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.DevOtaModuleAddInput{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.UpgradeDevOtaModule().AddDevOtaModule(ctx, data)
	return
}

// EditDevOtaModule 修改数据
func (u *cUpgradeDev_ota_module) EditDevOtaModule(ctx context.Context, req *upgrade.EditDevOtaModuleReq) (res *upgrade.EditDevOtaModuleRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.DevOtaModuleEditInput{}
	gconv.Scan(req, &data)
	// data.UpdateBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.UpgradeDevOtaModule().EditDevOtaModule(ctx, data)
	return
}

// DeleteDevOtaModule 删除数据
func (u *cUpgradeDev_ota_module) DeleteDevOtaModule(ctx context.Context, req *upgrade.DeleteDevOtaModuleReq) (res *upgrade.DeleteDevOtaModuleRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.UpgradeDevOtaModule().DeleteDevOtaModule(ctx, req.Ids)
	return
}
